﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Simple.Common.Pager;
using System.Web.Mvc;
using Simple.Common.Data;
using Simple.Entity;
using Simple.ViewModel;
using Simple.Entity.Models;
using Simple.Entity.Enums;
using Simple.Common.Core.Collections.Extensions;
using Simple.Common.Caching;

namespace Simple.Service
{
    public class CacheService : ApplicationService
    {
        private readonly WorkEfRepository<Sys_Role> _roleRepository;
        private readonly WorkEfRepository<Sys_DataDictionary> _dataDictionaryRepository;
        private readonly WorkEfRepository<Sys_Menu> _menuRepository;
        private readonly ICacheManager _cacheManager;


        public CacheService(WorkEfRepository<Sys_Role> roleRepository, WorkEfRepository<Sys_DataDictionary> dataDictionaryRepository, WorkEfRepository<Sys_Menu> menuRepository, ICacheManager cacheManager)
        {
            _roleRepository = roleRepository;
            _dataDictionaryRepository = dataDictionaryRepository;
            _menuRepository = menuRepository;
            _cacheManager = cacheManager;
        }

        /// <summary>
        /// 获取角色下拉列表数据源
        /// </summary>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public IList<SelectListItem> GetRoleList(long? Id = null)
        {
            var dataList = _cacheManager.Get<List<TextAndValue>>(CacheKeys.Sys_Role, () =>
            {
                var query = _roleRepository.TableNoTracking.Select(m => new TextAndValue
                {
                    Text = m.RoleName,
                    Value = m.Id.ToString()
                });
                return query.ToList();
            });
            if (Id != null)
                return dataList.ToSelectList(m => m.Text, m => m.Value, m => m.Value == Id.ToString());
            return dataList.ToSelectList(m => m.Text, m => m.Value);
        }

        private IList<DataDictionaryItem> GetDataDictionarys()
        {
            var dataList = _cacheManager.Get<List<DataDictionaryItem>>(CacheKeys.Sys_DataDictionary, () =>
            {
                return _dataDictionaryRepository.TableNoTracking.Select(m => new DataDictionaryItem
                {
                    Id = m.Id,
                    Title = m.Title,
                    Code = m.Code,
                    ParentId = m.ParentId
                }).ToList();
            });

            return dataList;
        }

        /// <summary>
        /// 获取字典分类集合
        /// </summary>
        /// <param name="parentCode">分类默认值</param>
        /// <returns></returns>
        public IList<SelectListItem> GetParentDataDictionaryList(long? Id = null)
        {
            var dataList = GetDataDictionarys().Where(m => m.ParentId == 0).Select(m => new TextAndValue
            {
                Text = m.Title,
                Value = m.Id.ToString()
            });

            if (Id != null)
                return dataList.ToSelectList(m => m.Text, m => m.Value, m => m.Value == Id.ToString());
            return dataList.ToSelectList(m => m.Text, m => m.Value);
        }

        private IList<MenuItem> GetMenus()
        {
            var dataList = _cacheManager.Get<List<MenuItem>>(CacheKeys.Sys_Menu, () =>
            {
                return _menuRepository.TableNoTracking.Select(m => new MenuItem
                {
                    MenuName = m.MenuName,
                    Id = m.Id,
                    MenuType = m.MenuType
                }).ToList();
            });

            return dataList;
        }

        /// <summary>
        /// 获取菜单模块集合
        /// </summary>
        /// <param name="Id">模块菜单ID</param>
        /// <returns></returns>
        public IList<SelectListItem> GetModularMenuList(long? Id = null)
        {
            var dataList = GetMenus().Where(m => m.MenuType == MenuTypeEnum.菜单模块).Select(m => new TextAndValue
            {
                Text = m.MenuName,
                Value = m.Id.ToString()
            }).ToList();

            if (Id != null)
                return dataList.ToSelectList(m => m.Text, m => m.Value, m => m.Value == Id.ToString());
            return dataList.ToSelectList(m => m.Text, m => m.Value);
        }

        /// <summary>
        /// 返回普通菜单集合
        /// </summary>
        /// <param name="Id">默认值</param>
        /// <returns></returns>
        public IList<SelectListItem> GetMenuList(long? Id = null)
        {
            var dataList = GetMenus().Where(m => m.MenuType == MenuTypeEnum.普通菜单).Select(m => new TextAndValue
            {
                Text = m.MenuName,
                Value = m.Id.ToString()
            }).ToList();

            if (Id != null)
                return dataList.ToSelectList(m => m.Text, m => m.Value, m => m.Value == Id.ToString());
            return dataList.ToSelectList(m => m.Text, m => m.Value);
        }

        /// <summary>
        /// 获取按钮类型列表
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IList<SelectListItem> GetActionTypeList(int? id = null)
        {
            id = id ?? 0;
            var actionTypeList = ActionTypeHelper.GetActionTypeList().Select(m => new TextAndValue
            {
                Text = m.Value,
                Value = m.Key.ToString()
            }).ToList();

            return actionTypeList.ToSelectList(m => m.Text, m => m.Value, m => m.Value == id.ToString());
        }
    }
}
